// bookCode project bookCode.go
package main //有func main package 如果不是main 则无法启动

import (
	"fmt"
)

type Integer int

func (a Integer) Less(b Integer) bool { //面向对象
	return a < b
}

func Integer_Less(a Integer, b Integer) bool { //面向过程
	return a < b
}

func (a Integer) Add(b Integer) {
	a += b
	fmt.Println("值方式传递 a=", a)
}
func (a *Integer) zzAdd(b Integer) {
	*a += b
	fmt.Printf("地址：%p，指针式传递 a=%v\n", a, *a)
	//fmt.Println("指针式传递 a=", a)
}

type Rect struct {
	x, y          float64
	width, height float64
}

func (r *Rect) Area() float64 {
	return r.width * r.height
}

//相当于构造函数
func NewRect(x, y, width, height float64) *Rect {
	return &Rect{x, y, width, height}
}

func main() {
	var a Integer = 1
	if a.Less(2) {
		fmt.Println(a, "Less 2")
	}
	Integer_Less(a, 2)
	//a.Add(3)
	a.zzAdd(3)
	fmt.Println("最终结果 a=", a)

	//初始化
	//rect1 := new(Rect)
	//rect2 := &Rect{}
	//rect3 := &Rect{0, 0, 100, 200}
	//rect4 := &Rect{width: 100, height: 200}
	rect5 := NewRect(1, 2, 3, 4)
	fmt.Println(rect5.Area())

	var file1 IFile = new(File)
	n, err := file1.Read(nil)
	fmt.Println(n, err)
	//var file2 IReader = new(File)
	//var file3 IWriter = new(File)
	//var file4 ICloser = new(File)
}

//相当于类
type File struct {
}

//接口
type IFile interface {
	Read(buf []byte) (n int, err error)
	Writer(buf []byte) (n int, err error)
	Seek(off int64, whence int) (pos int64, err error)
	Close() error
}
type IReader interface {
	Read(buf []byte) (n int, err error)
}
type IWriter interface {
	Writer(buf []byte) (n int, err error)
}
type ICloser interface {
	Close() error
}

//类实现了单个接口
func (f *File) Read(buf []byte) (n int, err error) {
	return 1, nil
}
func (f *File) Writer(buf []byte) (n int, err error) {
	return 1, nil
}
func (f *File) Seek(off int64, whence int) (pos int64, err error) {
	return 1, nil
}
func (f *File) Close() error {
	return nil
}

//匿名组合
type Base struct {
	Name string
}
type Foo struct {
	Base
}

func (base *Base) Foo() {

}
func (base *Base) Bar() {

}
func (foo *Foo) Bar() {
	foo.Base.Bar()
}
